package com.alibaba.citrus.cr.price.ability.functions.factory.price.process;

import com.alibaba.citrus.cr.common.annotation.RepositoryInvoker;
import com.alibaba.citrus.cr.common.exception.RepositoryException;
import com.epoch.app.bcorder.model.dto.BaselineBatchQueryChannelsRequest;
import com.epoch.app.bcorder.service.BaseDataService;
import com.epoch.app.crpricecenter.enums.DimensionPropertyEnum;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author hanguo
 * @date 2021/11/19
 * @Description 渠道
 */
@Component
public class ChannelProcess extends PriceProcess{
    /**
     * 基线能力
     */
    @Resource
    public BaseDataService baseDataService;

    @Override
    @RepositoryInvoker
    public Map<String, String> obtainCodeAndDescMap(List<String> codeList, boolean innerCode) {
        Map<String, String> channelCodeMap = new HashMap<>();

        List<Map<String, Object>> channelList = obtainChannel(codeList, innerCode);
        if (CollectionUtils.isNotEmpty(channelList)) {
            if (innerCode) {
                channelList.stream().filter(item -> Objects.nonNull(item) && StringUtils.isNotBlank((String) item.
                        get("channelCode")) && StringUtils.isNotBlank((String) item.get("channelName"))).forEach(item ->
                        channelCodeMap.put((String) item.get("channelCode"), (String) item.get("channelName"))
                );
            } else {
                channelList.stream().filter(item -> Objects.nonNull(item) && StringUtils.isNotBlank((String) item.
                        get("outCode")) && StringUtils.isNotBlank((String) item.get("channelName"))).forEach(item ->
                        channelCodeMap.put((String) item.get("outCode"), (String) item.get("channelName"))
                );
            }
        }

        return channelCodeMap;
    }

    @Override
    @RepositoryInvoker
    public Map<String, String> obtainInnerCodeAndOuterCodeMap(List<String> codeList, boolean innerCode) {
        Map<String, String> channelCodeMap = new HashMap<>();

        List<Map<String, Object>> channelList = obtainChannel(codeList, innerCode);
        if (CollectionUtils.isNotEmpty(channelList)) {
            if (innerCode) {
                channelList.stream().filter(item -> Objects.nonNull(item) && StringUtils.isNotBlank((String) item.
                        get("channelCode")) && StringUtils.isNotBlank((String) item.get("outCode"))).forEach(item ->
                        channelCodeMap.put((String) item.get("channelCode"), (String) item.get("outCode"))
                );
            } else {
                channelList.stream().filter(item -> Objects.nonNull(item) && StringUtils.isNotBlank((String) item.
                        get("outCode")) && StringUtils.isNotBlank((String) item.get("channelCode"))).forEach(item ->
                        channelCodeMap.put((String) item.get("outCode"), (String) item.get("channelCode"))
                );
            }
        }

        return channelCodeMap;
    }

    @RepositoryInvoker
    public List<Map<String, Object>> obtainChannel(List<String> codeList, boolean innerCode){
        BaselineBatchQueryChannelsRequest baselineBatchQueryChannelsRequest = new BaselineBatchQueryChannelsRequest();
        if (innerCode) {
            baselineBatchQueryChannelsRequest.setChannelCodes(codeList);
        } else {
            baselineBatchQueryChannelsRequest.setOutCodes(codeList);
        }
        Object channelResult = baseDataService.baselineBatchQueryChannels(baselineBatchQueryChannelsRequest);
        if (Objects.isNull(channelResult)) {
            throw new RepositoryException("OTS-07-010-01-17-001");
        }
        Map<String, Object> channelResultMap = (Map<String, Object>) channelResult;
        Map<String, Object> channelMap = (Map<String, Object>) channelResultMap.get("result");
        if (MapUtils.isEmpty(channelMap)) {
            return new ArrayList<>();
        }
        List<Map<String, Object>> channelList = (List<Map<String, Object>>) channelMap.get("result");
        if (CollectionUtils.isEmpty(channelList)) {
            return new ArrayList<>();
        }
        return channelList;
    }

    @Override
    public String getPropertyCode() {
        return DimensionPropertyEnum.CHANNEL_CODE.getKey();
    }
}
